<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/19 0019
 * Time: 10:31
 */

namespace frontend\controllers;
// header("Access-Control-Allow-Origin:http://medicals.qingguo188.com");
// header("Access-Control-Allow-Origin:http://localhost:8080");
header("Access-Control-Allow-Methods:POST,GET");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
header("Access-Control-Allow-Credentials: true");
use common\models\User;
use common\models\UserSecondary;
use common\models\UserDrug;
use common\models\UserDrugWarehouse;
use common\models\UserSupplier;
use Yii;
// use yii\web\Controller;
use yii\data\Pagination;
use frontend\controllers\BaseController;
/**
 * User controller
 */
class UserDrugController extends BaseController
{

    public function beforeAction($action)
    {
        if ($action->id == 'showlist' || $action->id == 'create' || $action->id == 'showupdate'|| $action->id == 'delete') {
            $this->enableCsrfValidation = false;
        }
        return parent::beforeAction($action);
    }
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [

        ];

    }
    /**
     * show
     *
     * return json
     */
    public function actionShowlist()
    {
        $session = \Yii::$app->session;
        $sessionInfo = $session->get('userInfo');
        if(Yii::$app->request->isPost){
            $data = Yii::$app->request->post();

            //判断用户级别 如果是2级用户的话,去查询一级用户的id
            if($data['level'] == 2){
                $secondlist = UserSecondary::find()->where(['id'=>$data['userid']])->one();
                if(!$secondlist){
                    return $this->getjson(0,'用户userid错误');
                }
                $data['userid'] = $secondlist['user_id'];
            }
            $total = UserDrug::find()->where("user_id=".$data['userid']." and name like '%".$data['name']."%'and status=1")->all();
            $shuju = $this->getPage($total,count($total),$data['page'],$data['pagesize']);
            //总页数
            $pages = ceil(count($total)/$data['pagesize']);
            $arr = [];
            $arr['totalpage'] = $pages;
            if($shuju){
                $countshuju = count($shuju);
                for($i = 0;$i<$countshuju;$i++){
                    $arr['list'][$i]['id']          = $shuju[$i]['id'];
                    $arr['list'][$i]['name']          = $shuju[$i]['name'];
                    $arr['list'][$i]['barcode']       = $shuju[$i]['barcode'];
                    $arr['list'][$i]['name_intro']    = $shuju[$i]['name_intro'];
                    $arr['list'][$i]['spell']         = $shuju[$i]['spell'];
                    $arr['list'][$i]['spell_intro']   = $shuju[$i]['spell_intro'];
                    $arr['list'][$i]['specification'] = $shuju[$i]['specification'];
                    $arr['list'][$i]['unit']          = $shuju[$i]['unit'];
                    $arr['list'][$i]['employ']        = $shuju[$i]['employ'];
                    $arr['list'][$i]['action']        = $shuju[$i]['action'];
                    $arr['list'][$i]['coding']        = $shuju[$i]['coding'];
                    $model1 = new UserSupplier;
                    $supplier = $model1::find()->where(['id'=>$shuju[$i]['vender_id']])->one();
                    $arr['list'][$i]['vendor'] = $supplier['name'];
                }
            }else{
                $arr['list'] = '';
            }
            //获得药品公司的信息
            $supplier = UserSupplier::find()->where(['user_id'=>$data['userid']])->all();
            if(count($supplier)){
                $countsupplier = count($supplier);
                for($j = 0;$j < $countsupplier;$j++){
                    $arr['supplier'][$j]['id'] = $supplier[$j]['id'];
                    $arr['supplier'][$j]['name'] = $supplier[$j]['name'];
                }
            }else{
                $arr['supplier'] = '';
            }
            return $this->getjson(1,$arr);
        }else{
            return $this->getjson(0,'后台接收参数失败,请重试');
        }
    }
    /**
     * create
     *
     * return json
     */
    public  function actionCreate()
    {
        if(Yii::$app->request->isPost){
            $data = Yii::$app->request->post();
            //判断用户级别 如果是2级用户的话,去查询一级用户的id
            if($data['level'] == 2){
                $secondlist = UserSecondary::find()->where(['id'=>$data['user_id']])->one();
                if(!$secondlist){
                    return $this->getjson(0,'用户userid错误');
                }
                $data['user_id'] = $secondlist['user_id'];
            }
            //判断该厂家是否存在
            $usersupplier = new UserSupplier();
            $list = $usersupplier::find()->where("name = :name",[':name'=>$data['supplier']])->one();
            if(!$list){
                $usersupplier->name = $data['supplier'];
                $usersupplier->user_id = $data['user_id'];
                if(!$usersupplier->save()){
                    return $this->getjson(0,'失败,请重试');
                }
                $vender_id = $usersupplier->id;
            }else{
                $vender_id = $list['id'];
            }

            $model = new UserDrug();
            if($data['id']){
                //修改
                $list = $model->findOne($data['id']);
                $list->name          = $data['name'];
                $list->name_intro    = $data['name_intro'];
                $list->specification = $data['specification'];
                $list->unit          = $data['unit'];
                $list->vender_id     = $vender_id;
                $list->coding     = $data['coding'];
                $list->employ     = $data['employ'];
                $list->action     = $data['action'];
                $list->spell  = $data['spell'];
                $list->spell_intro   = $data['spell_intro'];
                if($list->save()){
                    return $this->getjson(1,'成功');
                }else{
                    return $this->getjson(0,'失败,请重试');
                }
            }else{
                //更新
                $model->user_id       = $data['user_id'];
                $model->name          = $data['name'];
                $model->name_intro    = $data['name_intro'];
                $model->specification = $data['specification'];
                $model->unit          = $data['unit'];
                $model->vender_id     = $vender_id;
                $model->spell_intro   = $data['spell_intro'];
                $model->spell  = $data['spell'];
                $model->coding     = $data['coding'];
                $model->employ     = $data['employ'];
                $model->action     = $data['action'];
                if($model->save()){
                    return $this->getjson(1,'成功');
                }else{
                    return $this->getjson(0,'失败,请重试');
                }
            }
        }else{
            return $this->getjson(0,'后台接收参数失败,请重试');
        }
    }

    /**
     * create
     *
     * return json
     */
    public function actionShowupdate()
    {
        if(Yii::$app->request->isPost){
            $data = Yii::$app->request->post();
            if(!$data['id'] ||!$data['userid']){
                return $this->getjson(0,'参数错误');
            }
            $arr = [];
            $drugInfo = UserDrug::find()->where("id = :id and status = :status and user_id = :user_id",[':id'=>$data['id'],'status'=>1,':user_id'=>$data['userid']])->one();

            if(!$drugInfo){
                return $this->getjson(0,'参数错误,药品字典不存在');
            }
            if($drugInfo['vender_id']){
                $supplierInfo = UserSupplier::find()->where("user_id = :user_id and id = :id",[':user_id'=>$data['userid'],':id'=>$drugInfo['vender_id']])->one();
                if($supplierInfo){
                    $arr['info']['vender_name']   = $supplierInfo['name'];
                }
            }
            $arr['info']['user_id']       = $drugInfo['user_id'];
            $arr['info']['name']          = $drugInfo['name'];
            $arr['info']['name_intro']    = $drugInfo['name_intro'];
            $arr['info']['specification'] = $drugInfo['specification'];
            $arr['info']['unit']          = $drugInfo['unit'];
            $arr['info']['vender_id']     = $drugInfo['vender_id'];
            $arr['info']['spell_intro']   = $drugInfo['spell_intro'];


            $supplier = UserSupplier::find()->where("user_id = :user_id",[':user_id'=>$data['userid']])->all();
            if($supplier){
                $countsupplier = count($supplier);
                for($j = 0;$j < $countsupplier;$j++){
                    $arr['suppliers'][$j]['id'] = $supplier[$j]['id'];
                    $arr['suppliers'][$j]['name'] = $supplier[$j]['name'];
                }
            }
            return $this->getjson(1,$arr);
        }else{
            return $this->getjson(0,'后台接收参数失败,请重试');
        }
    }

    /**
     * 删除
     *
     * return json
     */
    public function actionDelete()
    {
        if(Yii::$app->request->isPost){
            $data = yii::$app->request->post();
            if($data){
                if($data['level'] == 2){
                    $secondlist = UserSecondary::find()->where(['id'=>$data['userid']])->one();
                    if(!$secondlist){
                        return $this->getjson(0,'用户userid错误');
                    }
                    $data['user_id'] = $secondlist['user_id'];
                }else{
                    $data['user_id'] = $data['userid'];
                }

                $model = UserDrug::findOne($data['drugid']);
                //先去删除用户药品仓库中的药品信息,然后才能删除该信息
                $user_drug_warehouser_list = UserDrugWarehouse::find()->where('user_id = :user_id and user_drug_id = :user_drug_id',[':user_id'=>$data['user_id'],':user_drug_id'=>$model->id])->all();
                foreach($user_drug_warehouser_list as $key => $val){
                    $userdrugwarehouser = new UserDrugWarehouse();
                    $warehouse = $userdrugwarehouser->findOne($val['id']);
                    $warehouse ->delete();
                }
                //删除字典信息
                if($model){
                    if($model->delete()){
                        return $this->getjson(1,'删除成功');
                    }else{
                        return $this->getjson(0,'删除失败,请重试');
                    }
                }else{
                    return $this->getjson(0,'该条记录不存在');
                }
            }else{
                return $this->getjson(0,'参数不能为空');
            }
        }else{
            return $this->getjson(0,'后台接收参数失败,请重试');
        }
    }

}




















